package com.gin.kotlin.no_gay_alliance.adapter

import android.content.Context
import android.content.DialogInterface
import android.util.Log
import android.view.View
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.AlertDialogUtils
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.callback.RCallback
import com.gin.kotlin.no_gay_alliance.database.entity.NgaAccount
import com.gin.kotlin.no_gay_alliance.databinding.ItemAccountBinding
import com.gin.kotlin.no_gay_alliance.enums.HandlerMessageWhat
import com.gin.kotlin.no_gay_alliance.manager.AccountManager
import com.gin.kotlin.no_gay_alliance.provider.ngaphp.BlockConfigProvider
import com.gin.nga.response.user.BlockConfigBody

class NgaAccountAdapter(context: Context, handler: MyHandler) : AbstractBindingRecyclerViewAdapter<ItemAccountBinding, NgaAccount>(context, handler), View.OnClickListener {
    /**
     * 当显示某一位置的item时的操作
     * @param holder ViewHolder
     * @param position Int
     * @param b ViewBinding
     * @param dataModel 数据对象
     */
    override fun onBindViewHolder(holder: ViewHolder<ItemAccountBinding>, position: Int, b: ItemAccountBinding, dataModel: NgaAccount) {
        b.tvUsername.text = dataModel.username
        b.tvUserId.text = dataModel.userId.toString()
        b.ivSelected.visibility = if (dataModel.enable) View.VISIBLE else View.GONE

        listOf(b.root, b.ivDelete).forEach {
            it.tag = dataModel
            it.setOnClickListener(this)
        }

    }

    override fun onClick(v: View?) {
        val account = v?.tag as? NgaAccount ?: return

        // 删除账号
        if (v.id == R.id.iv_delete) {
            if (!account.enable) {
                // 弹窗确认
                AlertDialogUtils.builder(context, "移除账号: ${account.username}", listener = { _, which ->
                    if (which == DialogInterface.BUTTON_POSITIVE) {
                        AccountManager.getInstance().delete(account.userId)
                        handler.sendEmptyMessage(HandlerMessageWhat.ACCOUNT_LIST_REFRESH)
                    }
                }).show()
            } else {
                handler.toast(context.getString(R.string.toast_message_can_not_delete_enable_account))
            }
        } else if (!account.enable) {
            // 登录账号
            AccountManager.getInstance().setEnableLogin(account.userId)
            handler.toast(context.getString(R.string.toast_message_account_logged, account.username))
            handler.sendEmptyMessage(HandlerMessageWhat.ACCOUNT_LIST_REFRESH)
            // 更新屏蔽数据
            BlockConfigProvider.getInstance().enqueue(callback = object : RCallback<BlockConfigBody, Nothing> {
                override fun onSuccess(data: BlockConfigBody, ignoreCache: Boolean) {
                    App.INSTANCE.blockConfig = data.data
                }

                override fun onError(errorMessage: String) {
                    Log.w(TAG, "屏蔽数据获取错误: $errorMessage")
                }
            })
        }

        // 登录该账号

    }
}